<template>
  <div class="audio-preview">
    <div class="audio-container" v-loading="loading">
      <div v-if="fileInfo.fileUrl" class="audio-player">
        <!-- 音频封面 -->
        <div class="audio-cover">
          <el-icon><VideoPlay /></el-icon>
        </div>
        
        <!-- 音频控制器 -->
        <audio 
          :src="fileInfo.fileUrl"
          controls
          preload="metadata"
          @loadedmetadata="handleAudioLoad"
          @error="handleAudioError"
          class="preview-audio"
        >
          您的浏览器不支持音频播放
        </audio>
      </div>
      
      <div v-else-if="error" class="error-message">
        <el-icon><VideoPlay /></el-icon>
        <p>音频加载失败</p>
      </div>
    </div>
    
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { VideoPlay } from '@element-plus/icons-vue'

const props = defineProps({
  fileInfo: {
    type: Object,
    default: () => ({})
  }
})

const loading = ref(true)
const error = ref(false)

// 音频加载完成
const handleAudioLoad = (event) => {
  loading.value = false
  error.value = false
}

// 音频加载错误
const handleAudioError = () => {
  error.value = true
  loading.value = false
}

onMounted(() => {
  // 初始化时设置加载状态
  if (props.fileInfo.fileUrl) {
    loading.value = true
    error.value = false
  }
})
</script>

<style scoped>
.audio-preview {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.audio-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
  border-radius: 8px;
  min-height: 400px;
  position: relative;
}

.audio-player {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
}

.audio-cover {
  width: 120px;
  height: 120px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 48px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.preview-audio {
  width: 300px;
  outline: none;
}

.error-message {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #999;
  font-size: 14px;
}

.error-message .el-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

</style>
